<?php
class Media_model extends CI_Model {

    var $source = '';
    var $source_id = '';
    var $owner_id = '';
    var $title = '';
    var $date = '';
    
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function get_media($media_id)
    {
        $this->db->select('*');
        $this->db->where('media_id', $media_id);
        $this->db->from('videos_videos');   
        $query = $this->db->get();
        return $query->first_row('array');
    }
    
    
    function get_all_media()
    {
        $query = $this->db->get('videos_videos');
        return $query->result();    
    }
    
    function get_media_by_query($query_string)
    {
        $this->db->select('*');
        $this->db->like('title', $query_string);
        $this->db->from('videos_videos');   
        $query = $this->db->get();
        return $query->result('array');
    }
    
    
    function get_all_media_by_user($user_id)
    {
        $this->db->select('*');
        $this->db->where('owner_id', $user_id);
        $this->db->from('videos_videos');     
        $query = $this->db->get();
        return $query->result();    
    }
    function get_all_media_by_tag($tag_id)
    {  
        $this->db->select('*');
        $this->db->join('videos_tags', 'videos_tags.media_id = videos_videos.media_id');
        $this->db->where('videos_tags.tag_id', $tag_id);
        $this->db->from('videos_videos');     
        $query = $this->db->get();
        return $query->result('array');        
    }
    function update_media()
    {
        $source_id = $this->input->post('source_id');
        
        $index = strpos( $source_id, "v=");
        if($index > 0)
            $source_id = substr ( $source_id , $index +2, 11);
        
        $data = array(
               'source_id' => $source_id ,
               'source' => $this->input->post('source') ,
               'title' => $this->input->post('title')
            );
        
        $this->db->where('media_id', $this->input->post('media_id'));
        $this->db->update('videos_videos', $data);
    }
    function save_media()
    {        
        $source_id = $this->input->post('source_id');
        
        $index = strpos( $source_id, "v=");
        if($index > 0)
            $source_id = substr ( $source_id , $index +2, 11);
        
        $data = array(
               'source_id' => $source_id ,
               'source' => $this->input->post('source') ,
               'owner_id' => $this->session->userdata('user_id') ,
               'title' => $this->input->post('title')
            );
        
        $this->db->insert('videos_videos', $data);
    }
     
}
?>